<?php defined('BASEPATH') or exit('No direct script access allowed');

class Paypal_m extends MY_Model {	
	var $pp = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
	var $stt = 'sandbox';
	var $mcEmal = 'hungdd_1308462165_biz@yahoo.com';
	var $itemName = '';
	
	function __construct(){
		$this->stt = 'sandbox';
		
		if( $this->stt == 'sandbox' ){
			$this->pp = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
			$this->mcEmal = 'hungdd_1308462165_biz@yahoo.com';
			$this->itemName = 'Paypal test purchase';
		}elseif ( $this->stt == 'paypal' ){
			$this->pp = 'https://www.paypal.com/cgi-bin/webscr';
			$this->mcEmal = '';
			$this->itemName = 'Paypal Transaction';
		}
		parent::__construct();
	}
	
	function updateInformation($user_id, $amount){
		$USER = $this->user_entity->init('id',$user_id) ;
		
		if(!isUserLogin()){
			redirect(site_url());exit();
		}
		
		$myCashUSD = $USER->convertToUSD() + changeValueOfMoney ( $amount, 'usd', 'inr' );
		 
		$USER->setCash( changeValueOfMoney( $myCashUSD, $USER->getCurrencyCode(), 'usd' ) );
		$USER->update();
		
	/**	$transactionId = $this->transaction_entity->createNewEntity();
		$TRANSACTION = $this->transaction_entity->init('id', $transactionId);
		$TRANSACTION->setUserId($USER->getId());
		$TRANSACTION->setPackageId($package_id);
		$TRANSACTION->setType('paypal');
		$TRANSACTION->update();
		**/
	}
	
	function process(){
		if(!isUserLogin()){
			redirect(site_url());exit();
		}
		
		require_once('paypal.class.php'); 
		$p = new paypal_class;            
		$p->paypal_url = $this->pp;
		
		$p->add_field('business', $this->mcEmal );
		$p->add_field('return', site_url('cricket/paypal/return_listener') );
		$p->add_field('cancel_return', site_url('cricket/paypal/cancel_listener') );
		$p->add_field('notify_url',site_url('cricket/paypal/notify_listener') ); 
		$p->add_field('item_name', $this->itemName );
		$p->add_field('amount', $_SESSION['deposit_amount'] );
		$p->add_field('currency_code', 'INR' );	
		  
		$p->submit_paypal_post(); 
	}
	
	function return_listener(){
		if( !isset($_SESSION['paypal_done']) || $_SESSION['paypal_done'] != 'paid' ){
			$this->updateInformation(getUserId(), $_SESSION['package_id']);
			$_SESSION['paypal_done'] = 'paid';
			redirect( site_url('cricket/account/deposit_notify?s=OK') );
			exit();
		}	
		redirect( site_url('cricket/account/deposit_notify?s=OK') );
	}
	
	function cancel_listener(){
		unset($_SESSION['paypal_done']);
		unset($_SESSION['package_id']);
		unset($_SESSION['deposit_amount']);
		redirect ( site_url('cricket/account/deposit_notify?s=CANCEL') );
		exit();
	}
	
	function notify_listener(){
		require_once('paypal.class.php'); 
		$p = new paypal_class;            
		
		if ( $p->validate_ipn($this->stt) ){
	      	$this->updateInformation(getUserId(), $_SESSION['deposit_amount']);
			$_SESSION['paypal_done'] = 'paid';
			redirect( site_url('cricket/account/deposit_notify?s=OK') );
			exit();
      	}
	}
	
	function updateInformationAddcoins($user_id, $package_id){
		$USER = $this->user_entity->init('id',$user_id) ;
		$PACKAGE = $this->coin_packages_entity->init('id', $package_id);	
		
		if(!$PACKAGE || !isUserLogin()){
			redirect(site_url());exit();
		}
		 
		$USER->setCoin( $USER->getCoin() + $PACKAGE->getCoin() );
		$USER->update();
		
		$transactionId = $this->transaction_entity->createNewEntity();
		$TRANSACTION = $this->transaction_entity->init('id', $transactionId);
		$TRANSACTION->setUserId($USER->getId());
		$TRANSACTION->setPackageId($package_id);
		$TRANSACTION->setType('paypal');
		$TRANSACTION->update();
	}
	
	function process_addcoins(){
		$package_id = intval($this->input->get('pid',TRUE));
		$PACKAGE = $this->coin_packages_entity->init('id', $package_id);
		
		if(!$PACKAGE || !isUserLogin()){
			redirect(site_url());exit();
		}
		
		$_SESSION['package_id_addcoins'] = $package_id;
		
		require_once('paypal.class.php'); 
		$p = new paypal_class;            
		$p->paypal_url = $this->pp;
		
		$p->add_field('business', $this->mcEmal );
		$p->add_field('return', site_url('cricket/paypal/return_addcoins_listener') );
		$p->add_field('cancel_return', site_url('cricket/paypal/cancel_addcoins_listener') );
		$p->add_field('notify_url',site_url('cricket/paypal/notify_addcoins_listener') ); 
		$p->add_field('item_name', $this->itemName );
		$p->add_field('amount', $PACKAGE->getCost() );
		$p->add_field('currency_code', 'INR' );	
		  
		$p->submit_paypal_post(); 
	}
	
	function return_addcoins_listener(){
		if( !isset($_SESSION['paypal_addcoins_done']) || $_SESSION['paypal_addcoins_done'] != 'paid' ){
			$this->updateInformationAddcoins(getUserId(), $_SESSION['package_id_addcoins']);
			$_SESSION['paypal_addcoins_done'] = 'paid';
			redirect( site_url('cricket/account/addcoin_paypal_notify?s=OK') );
			exit();
		}	
		redirect( site_url('cricket/account/addcoin_paypal_notify?s=OK') );
	}
	
	function cancel_addcoins_listener(){
		unset($_SESSION['paypal_addcoins_done']);
		unset($_SESSION['package_id_addcoins']);
		redirect ( site_url('cricket/account/addcoin_paypal_notify?s=CANCEL') );
		exit();
	}
	
	function notify_addcoins_listener(){
		require_once('paypal.class.php'); 
		$p = new paypal_class;            
		
		if ( $p->validate_ipn($this->stt) ){
	      	$this->updateInformationAddcoins(getUserId(), $_SESSION['package_id_addcoins']);
			$_SESSION['paypal_addcoins_done'] = 'paid';
			redirect( site_url('cricket/account/addcoin_paypal_notify?s=OK') );
			exit();
      	}
	}
//end class
}	